<?php

namespace app\common\controller;

use think\Config;
use think\Loader;
use think\Request;
use think\Exception;
use think\Controller;
use app\common\traits\controller\TraitController;

abstract class Api extends Controller
{
    use TraitController;

    protected $user = null;

    protected function init()
    {
        $this->user = $this->getLoginUser();
        Request::instance()->bind('user', $this->user);
    }

    protected function getLoginUser(){}

    /**
     * 获取会员房车
     * @return [type] [description]
     */
    protected function getUsercar()
    {
        return $this->user->getUserCaravans();
    }

    /**
     * 获取房车信息
     * @param  [type] $caravan_id 用户房车id
     * @return [type]             [description]
     */
    protected function getCaravan($caravan_id)
    {
        return $this->user->getCaravanInfo($caravan_id);
    }

    /**
     * 修改用户房车信息
     * @param [type] $caravan_id [description]
     * @param [type] $infos      [description]
     */
    protected function setCaravanInfo($caravan_id, $infos)
    {
        return $this->user->updateCaravanInfo($id, $infos);
    }

    /**
     * 检索房车
     * @param  [type] $car_area 地区
     * @param  [type] $sdate    出行开始时间
     * @param  [type] $edate    出行结束时间
     * @return [type]           [description]
     */
    protected function search($car_area, $sdate = null, $edate = null)
    {
        return Loader::model('Caravans')->search(['car_area' => $car_area, 'sdate' => $sdate, 'edate' => $edate]);
    }

    /**
     * 获取用户资金信息
     * @return [type] [description]
     */
    protected function getUsercapital()
    {
        return $this->user->getUserCapitals();
    }

    /**
     * 获取用户订单列表
     * @return [type] [description]
     */
    protected function getUserorders($querys = [])
    {
        return $this->user->getOrders($querys);
    }

    /**
     * 创建订单
     * @param  [type] $caravan_id     选择的房车id
     * @param  [type] $take_car_date  开始时间
     * @param  [type] $leave_car_date 结束时间
     * @param  array  $other          []
     * @return [type]                 [description]
     */
    protected function createOrder($caravan_id, $take_car_date, $leave_car_date, $other = [])
    {
        return $this->user->createOrder($caravan_id, $take_car_date, $leave_car_date, $other);
    }

    /**
     * 获取订单信息
     * @param  [type] $number 订单number
     * @return [type]         [description]
     */
    protected function getOrder($number)
    {
        return $this->user->getOrder($number);
    }

    /**
     * 支付订单
     * @param  [type] $number [description]
     * @return [type]         [description]
     */
    protected function payorder($number)
    {
        return $this->user->payOrder($number);
    }

    /**
     * 取消订单
     * @param  [type] $number [description]
     * @return [type]         [description]
     */
    protected function cancelorder($number)
    {
        return $this->user->cancelOrder($number);
    }

    /**
     * 初始化用户房车
     * @param  [type] $caravan_id 用户房车id
     * @param  [type] $start_date 分享的热门日期开始
     * @param  [type] $end_date   分享的热门日期结束
     * @return [type]             [description]
     */
    protected function intcar($caravan_id, $start_date, $end_date)
    {        
        if (!$this->user->getCaravanInfo($caravan_id)->checkPeakDate($start_date, $end_date)) {
            throw new Exception(\think\Lang::get("Please share peak time"), 50001); 
        }

        $queue_data = [
            'caravan_id' => $caravan_id, 
            'user_id'    => $this->user->id,
            'start_date' => $start_date, 
            'end_date'   => $end_date
        ];

        try {
            \think\Queue::push('common/Caravan@intcar', $queue_data);
        } catch (\Exception $e) {
            return false;
        }

        return true;
        // return $this->user->getCaravanInfo($caravan_id)->initCaravanCarlendar(['start_date' => $start_date, 'end_date' => $end_date]);
    }

    /**
     * 设置分享
     * @param [type] $caravan_id [description]
     * @param [type] $start_date [description]
     * @param [type] $end_date   [description]
     */
    protected function setShare($caravan_id, $start_date, $end_date)
    {
        return $this->user->getCaravanInfo($caravan_id)->setCaravanShare($start_date, $end_date);
    }

    /**
     * 设置自用
     * @param [type] $caravan_id [description]
     * @param [type] $start_date [description]
     * @param [type] $end_date   [description]
     */
    protected function setHome($caravan_id, $start_date, $end_date)
    {
        return $this->user->getCaravanInfo($caravan_id)->setCaravanHome($start_date, $end_date);
    }

    /**
     * 设置托管
     * @param [type] $caravan_id [description]
     * @param [type] $start_date [description]
     * @param [type] $end_date   [description]
     */
    protected function setTrust($caravan_id, $start_date, $end_date)
    {
        return $this->user->getCaravanInfo($caravan_id)->setCaravanTrusteeship($start_date, $end_date);
    }

    /**
     * 创建心愿
     * @param  [type] $region      期望地区
     * @param  [type] $start_date  开始时间
     * @param  [type] $end_date    结束时间
     * @param  [type] $travel_coin 奖励旅行币
     * @return [type]              [description]
     */
    protected function createwish($wish_info)
    {
        return $this->user->createWish($wish_info);
    }

    /**
     * 获取心愿信息
     * @param  [type] $id 心愿id
     * @return [type]     [description]
     */
    protected function wishInfo($id)
    {
        return $this->user->getWishInfo($id);
    }

    /**
     * 获取用户自己的心愿列表
     * @return [type] [description]
     */
    protected function getWishs()
    {
        return $this->user->getWishs();
    }

    /**
     * 获取其他人的心愿
     * @return [type] [description]
     */
    protected function getOtherwishs($querys = [],$page=1)
    {
        return $this->user->getOtherUserWishs($querys,$page);
    }

    /**
     * 获取通知数量(1:已读 0：未读)
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    protected function getNoticeCount($status)
    {
        return $this->user->getNoticeCount($status);
    }

    /**
     * 设置所有未读信息为已读
     */
    protected function setAllNoticesIsRead()
    {
        return $this->user->setAllNoticesIsRead();
    }

    /**
     * 获取通知列表
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    protected function getNoticeList()
    {
        return $this->user->getNoticeList();
    }

    /**
     * 删除通知
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    protected function delNotice($ids)
    {
        return $this->user->delNotice($ids);
    }

    /**
     * 结束订单 管理员操作
     * @param [type] $number [description]
     */
    protected function setEndorder($number)
    {
        return $this->user->endOrder($number);
    }

    /**
     * 提现申请
     * @param  [type] $amount    提现金额
     * @param  [type] $bank_num  卡号
     * @param  [type] $open_bank 开户行
     * @param  string $currency  [description]
     * @return [type]            [description]
     */
    protected function applyWithdraw($amount, $bank_num, $open_bank, $currency = 'CNY')
    {
        return $this->user->applyWithdraw($amount, ['bank_num' => $bank_num, 'open_bank' => $open_bank], $currency);
    }

    public function __get($name)
    {
        try {
            return Loader::model($name);
        } catch (\Exception $e) {
            throw $e;
        }
    }
}
